package com.xiaoqi.api.handle;

import com.xiaoqi.api.utlis.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

//@RestControllerAdvice
/**
 * 步骤：1,加上注解@RestControllerAdvice
 */
public class GlobalExceptionHandler {
    //这个方法是如果抛出这个NoHandlerFoundException异常，会执行这个方法
    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<String> handlerNotFoundException(Exception exception){
        Result<String> result = new Result();
        result.setCode(404);
        result.setMsg("路径出错，请检查请求信息！");
        return result;
    }

    //这个方法是如果抛出这个(Exception异常，会执行这个方法
    @ExceptionHandler(Exception.class)
    public Result<String> handlerException(Exception exception){
        Result<String> result = new Result();
        result.setCode(500);
        result.setMsg("服务器繁忙！");
        return result;
    }
}
